package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.SComment;
import com.example.entity.User;
import com.example.mapper.SCommentMapper;
import com.example.service.SCommentService;
import com.example.service.SOrderService;
import com.example.service.UserService;
import com.example.vo.SCommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SCommentServiceImpl extends ServiceImpl<SCommentMapper, SComment> implements SCommentService {
    @Autowired
    SCommentMapper sCommentMapper;
    @Autowired
    UserService userService;

    @Autowired
    SOrderService sOrderService;
    @Override
    public List<SCommentVO> getAllCommentBySpuId(Long spuId,Integer option) {
//        LambdaQueryWrapper<SComment> eq1 = new QueryWrapper<SComment>().lambda().eq(SComment::getSpuId, spuId);
//
//        List<SComment> sComments = sCommentMapper.selectList(eq1);
        List<SComment> commentBySpuId = sCommentMapper.getCommentBySpuId(spuId, option);
        List<SCommentVO> sCommentVOs = new ArrayList<>();
        for (SComment sComment : commentBySpuId) {
            Long userId = sComment.getUserId();
            User userById = userService.findById(userId);

            SCommentVO sCommentVO = new SCommentVO();
            sCommentVO.setUserName(userById.getUsername());
            sCommentVO.setAvatar(userById.getAvatar());
            sCommentVO.setDetail(sComment.getDetail());
            sCommentVO.setContent(sComment.getContent());
            sCommentVO.setCommentTime(sComment.getCreateTime());
            sCommentVO.setRate(sComment.getRate());

            sCommentVOs.add(sCommentVO);
        }

        return sCommentVOs;
    }
}
